package com.mikey.springcloud.controller;

import com.mikey.springcloud.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author :Mikey
 * @description :
 * @create :2022-08-29 21:25:00
 */
@RestController
public class CategoryController {
    @Autowired
    private RestTemplate restTemplate;

    //private static final String REST_URL_PREFIX = "http://localhost:8090";
    //通过服务名字来访问
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-CATEGORY";


    @RequestMapping("/consumer/category/add")
    public int add(@RequestBody Category category) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/category/add", category, int.class);
    }

    @RequestMapping("/consumer/category/delete/{id}")
    public int delete( @PathVariable("id") Integer id) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/category/delete", id, int.class);
    }

    @RequestMapping("/consumer/category/update")
    public int update(@RequestBody Category category) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/category/update", category, int.class);
    }

    @RequestMapping("/consumer/category/select/{id}")
    public Category selectOne(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/category/select/"+id, Category.class);
    }

    @RequestMapping("/consumer/category/list")
    public List<Category> list() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/category/list", List.class);
    }


}
